/*
 * FillPainter.java
 *
 * Created on August 8, 2007, 8:50 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.bff.mpd.musicjukebox.ui.components;

import java.awt.AlphaComposite;
import java.awt.Color;
import java.awt.GradientPaint;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.InputStream;

/**
 *
 * @author Bill
 */
public class FillPainter extends WatermarkPainter {
    
    /** The image to paint in the background */
    private Image bgImage;
    
    public FillPainter() {
        //bgImage = getImage(getClass().getResource("resources/blueguitar.jpg"));
        InputStream inStream = this.getClass().getResourceAsStream("/resources/mpd/blueguitar.jpg");
        bgImage = loadImage(inStream); 
    }
    
    public String[] getCommands() {
        return new String[] {""};
    }
    
    public void paint(Graphics g) {
        Graphics2D g2d = (Graphics2D)g;
        // if a background image exists, paint it
        if (bgImage != null) {
            int width = getComponent().getWidth();
            int height = getComponent().getHeight();
            int imageWidth = bgImage.getWidth(null);
            int imageHeight = bgImage.getHeight(null);
            
            float opacity = 0.4f;
            float fadeHeight = 1f;
            
            // we'll tile the image to fill our area
           for (int x = 0; x < width; x += imageWidth) {
               for (int y = 0; y < height; y += imageHeight) {
                    g2d.setPaint( new GradientPaint( 0, 0, Color.lightGray, 0, height, Color.darkGray ) );
                    g2d.fillRect( 0, 0, width, height );
                    
                    BufferedImage fadedImage = new BufferedImage( imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB );
                    Graphics2D rg = fadedImage.createGraphics();
                    
                    rg.drawRenderedImage( (BufferedImage)bgImage, null );
                    rg.setComposite( AlphaComposite.getInstance( AlphaComposite.DST_IN ) );
                    rg.setPaint(new GradientPaint(
                            0, imageHeight*fadeHeight, new Color( 0.0f, 0.0f, 0.0f, 0.0f ),
                            0, imageHeight, new Color( 0.0f, 0.0f, 0.0f, opacity )
                            )
                            );
                    
                    rg.fillRect( 0, 0, imageWidth, imageHeight );
                    rg.dispose();
                    g2d.drawRenderedImage( fadedImage, null );
        /*
                    //g.drawImage(bgImage, x, y, getComponent());
         
                    BufferedImage reflection = new BufferedImage( imageWidth, imageHeight, BufferedImage.TYPE_INT_ARGB );
                    //Graphics2D rg = reflection.createGraphics();
                    //g.drawImage(bgImage, x, y6, getComponent());
                    Graphics2D g2d = (Graphics2D)(bgImage.getGraphics());
                    g2d.drawRenderedImage( (BufferedImage)bgImage, null );
                    //g2d.setComposite( AlphaComposite.getInstance( AlphaComposite.DST_IN ) );
                    //g2d.setPaint( new GradientPaint( 0, imageHeight*fadeHeight, new Color( 0.0f, 0.0f, 0.0f, 0.0f ), 0, imageHeight, new Color( 0.0f, 0.0f, 0.0f, opacity ) ) );
                    //g2d.fillRect( 0, 0, imageWidth, imageHeight );
                    //g2d.dispose();
                }
            }
         */
                }
            }
        }
    }
    
    public void actionPerformed(ActionEvent ae) {
        bgImage = getImage(getClass().getResource(ae.getActionCommand()));
        getComponent().repaint();
    }
    
}

